<template>
  <div class="login-container">
    <div class="login-box">
      <el-card class="login-card">
        <div class="login-header">
          <img src="../assets/logo.png" alt="Logo" class="logo" />
          <h1>大棚监控系统</h1>
          <p class="subtitle">智能农业管理平台</p>
        </div>
        <el-form
          :model="loginForm"
          @submit.prevent="handleLogin"
          class="login-form"
        >
          <el-form-item>
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleLogin"
              :loading="loading"
              class="login-button"
              size="large"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '../api'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const loginForm = ref({
  username: '',
  password: ''
})

const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  try {
    loading.value = true
    const response = await login(loginForm.value)
    const token = response.data.data.token
    console.log('Received token:', token)

    localStorage.setItem('token', token)
    console.log('Stored token:', localStorage.getItem('token'))

    ElMessage.success('登录成功')
    router.push('/dashboard')
  } catch (error) {
    console.error('Login error:', error)
    ElMessage.error('登录失败：' + (error.response?.data?.message || '用户名或密码错误'))
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1abc9c 0%, #8e44ad 100%);
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.1);
  transform: rotate(45deg);
  top: -50%;
  left: -50%;
}

.login-box {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 440px;
  padding: 0 20px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  padding: 40px 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
}

.login-header h1 {
  font-size: 28px;
  color: #2c3e50;
  margin: 0;
  font-weight: 600;
}

.subtitle {
  color: #7f8c8d;
  margin-top: 10px;
  font-size: 16px;
}

.login-form {
  margin-top: 30px;
}

.login-form :deep(.el-input) {
  --el-input-hover-border: #1abc9c;
  --el-input-focus-border: #1abc9c;
}

.login-form :deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  background: linear-gradient(to right, #1abc9c, #2ecc71);
  border: none;
  margin-top: 20px;
  transition: transform 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(26, 188, 156, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

@media (max-width: 480px) {
  .login-box {
    padding: 0 15px;
  }

  .login-card {
    padding: 30px 20px;
  }

  .login-header h1 {
    font-size: 24px;
  }

  .subtitle {
    font-size: 14px;
  }
}
</style>